//
//  DBPhotoSeletorCell.swift
//  photoSelector
//
//  Created by 大波 on 16/3/11.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit

protocol DBPhotoSeletorCellDelegate: NSObjectProtocol {
    
    func addButtonDidClick(cell: DBPhotoSeletorCell, button: UIButton)
    
    func deletedButtonDidClick(cell: DBPhotoSeletorCell, button: UIButton)
}

class DBPhotoSeletorCell: UICollectionViewCell {
    
    /// 代理属性
    weak var photoSeletorCellDelegate: DBPhotoSeletorCellDelegate?
    
    var image: UIImage? {
        
        didSet {
            
            addButton.setImage(image, forState: UIControlState.Normal)
            
            deletedButton.hidden = false
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareCollectionViewCell()
    }
    
    private func prepareCollectionViewCell() {
        
        contentView.addSubview(addButton)
        
        contentView.addSubview(deletedButton)
        
        addButton.translatesAutoresizingMaskIntoConstraints = false
        
        deletedButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[ab]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["ab" : addButton]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[ab]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["ab" : addButton]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[db]-6-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["db" : deletedButton]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-6-[db]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["db" : deletedButton]))
    }
    
    func setAddButton() {
        
        addButton.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        
        deletedButton.hidden = true
    }
    
    
    // MARK: - 按钮点击事件
    
    func addButtonDidClick(button: UIButton) {
        
        photoSeletorCellDelegate?.addButtonDidClick(self, button: button)
    }
    
    @objc private func deletedButtonDidClick(button: UIButton) {
        
        photoSeletorCellDelegate?.deletedButtonDidClick(self, button: button)
    }
    
    
    // MARK: - 懒加载
    
    private lazy var addButton: UIButton = {
    
        let add = UIButton()
        
        add.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        
        add.addTarget(self, action: "addButtonDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return add
    }()
    
    private lazy var deletedButton: UIButton = {
    
        let del = UIButton()
        
        del.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        
        del.addTarget(self, action: "deletedButtonDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return del
    }()
}
